<template>
  <div class="app-container">
    <!--  查询  -->
    <el-card>
      <el-form :inline="true" :model="formInline" class="demo-form-inline formHeight">
        <el-form-item label="车牌号">
          <el-input v-model="formInline.car_num" size="mini" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="search">查询</el-button>
          <el-button type="primary" size="mini" @click="addOrEdit(1)">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!--  列表  -->
    <el-card class="mt20">
      <el-table
        :data="tableData"
        style="width: 100%;"
        class="tHeight"
        size="mini"
        :default-sort="{prop: formInline.sort, order: formInline.order}"
        @sort-change="tableSortChange"
      >
        <el-table-column
          property="car_num"
          label="车牌号"
          align="center"
        />
        <el-table-column
          property="car_type"
          label="车型"
          align="center"
        />
        <el-table-column
          property="car_use"
          label="用途"
          align="center"
        />
        <el-table-column
          property="car_config"
          label="是否配司机"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.car_config === 0 ? 'danger' : 'success' ">{{ scope.row.car_config === 0 ? '否' : '是' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          property="car_driver"
          label="司机"
          align="center"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="warning"
              size="mini"
              @click="addOrEdit(2, scope.row.id)"
            >
              修改
            </el-button>
            <el-button
              type="danger"
              size="mini"
              @click="del(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--   分页   -->
      <el-pagination
        style="float: right;"
        :page-size="10"
        :current-page="formInline.offset"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <div v-if="detailFlag">
      <Detail
        :id="detailId"
        :dialog-visible="detailFlag"
        :title="detailTitle"
        @close="close"
        @confirm="confirm"
      />
    </div>

  </div>
</template>

<script>
import { carList, deleteCar } from '@/api/operations'
import Detail from './components/detail'

export default {
  name: 'companyUser',
  components: {
    Detail
  },
  data() {
    return {
      detailId: 0, // 修改详情id
      detailFlag: false, // 详情弹窗标识
      detailTitle: '新增', // 弹窗标识
      tableData: [], // 列表数据
      total: 0, // 数据总条数
      // 搜索表单
      formInline: {
        sort: 'created_at',
        offset: 0,
        limit: 10,
        order: 'ascending',
        car_num: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
       * @description: 排序
       * @author: 书童
       */
    tableSortChange(column) {
      this.formInline.order = column.order ? column.order : 'descending'
      this.formInline.sort = column.prop
      this.formInline.offset = 0
      this.getList()
    },
    /**
       * @description: 查询
       * @author: 书童
       */
    search() {
      this.formInline.offset = 0
      this.getList()
    },
    /**
       * @description: 删除数据
       * @author: 书童
       * @param: id
       */
    del(id) {
      console.log(id)
      this.$confirm('是否要删除此条数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCar({ id }).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        })
      })
    },
    /**
       * @description: 新增或编辑
       * @author: 书童
       * @param: 1新增，2编辑
       */
    addOrEdit(num, id) {
      if (num === 1) {
        this.detailTitle = '新增'
        this.detailFlag = true
      } else {
        this.detailTitle = '编辑'
        this.detailId = id
        this.detailFlag = true
      }
    },
    // 获取列表
    getList() {
      carList(this.formInline).then(res => {
        this.tableData = res.data.groups
        this.total = res.data.total
      })
    },
    //  关闭弹窗组件
    close() {
      this.detailFlag = false
      this.detailId = ''
    },
    // 弹窗返回
    confirm() {
      this.close()
      this.getList()
    },
    handleSizeChange(val) {
      this.formInline.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.formInline.offset = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
  .formHeight{
    height: 40px;
  }
  .mt20{
    margin-top: 20px;
  }
  .tHeight{
    /*height: calc(100vh - 350px);*/
    overflow: auto;
  }
</style>
